<?php

class IstanbulPharmacyParser 
{
	
	private $rawdata;
	private $coordinateOffset = 0;  /* this is the offset when finding latitude and longitude in string, it is initialized in finding first element and 
					increased regularly in finding every element to find the next element more quickly*/
				     		
	
	public function parsePharmacies($rawdata)
	{	
		$this->rawdata = $rawdata;
		$dom = new DOMDocument('1.0', 'iso-8859-9');
		$dom->loadHTML($this->rawdata); // burada loadHtmlFile kullanilabilir.
		$pharmacyList = array();
		
		$domContainerPanel = $dom->getElementsByTagName("table"); //->getElementsByTagName("tr");
	
		$pharmacies = $domContainerPanel->item(0)->getElementsByTagName("table");
		$numberOfItems = $pharmacies->item(0)->getElementsByTagName("tr")->length;
		$j=0;
		while($j < $numberOfItems) {
			$pharmacyName = iconv("UTF-8", "ISO-8859-1", $pharmacies->item(0)->getElementsByTagName("tr")->item($j)->nodeValue);
			$pharmacyAddress = iconv("UTF-8", "ISO-8859-1", $pharmacies->item(0)->getElementsByTagName("tr")->item($j+2)->getElementsByTagName("td")->item(1)->nodeValue);
			$pharmacyPhone = '0'.$pharmacies->item(0)->getElementsByTagName("tr")->item($j+3)->getElementsByTagName("td")->item(1)->nodeValue;
	
			if($pharmacies->item(0)->getElementsByTagName("tr")->item($j+5) != null &&
				$pharmacies->item(0)->getElementsByTagName("tr")->item($j+5)->getElementsByTagName("a")->item(0) != null) 
			{
				$pharmacyURL = $pharmacies->item(0)->getElementsByTagName("tr")->item($j+5)->getElementsByTagName("a")->item(0)->getAttribute('href');
				$j += 6;
			}
			else {
				$pharmacyURL = 0;
				$j += 5;
			}	
			array_push($pharmacyList, new Pharmacy($pharmacyName, $pharmacyPhone, $pharmacyAddress, 0, 0, $pharmacyURL));
		}		
		return $pharmacyList;
	}
	
}

?>